/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.TreeSingleBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class TreeSingleXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_item = false;
	private boolean in_mid = false;
	private boolean in_name = false;
	private boolean in_date = false;
	private boolean in_description = false;
	private boolean in_tree_name = false;
//	private boolean in_today_score = false;
//	private boolean in_fruit_get = false;
//	private boolean in_fruit_start = false;
//	private boolean in_fruit_end = false;
	
	private boolean in_result = false;
	
	private List<TreeSingleBean> list;
	private TreeSingleBean mSingleInfo;
//	private String title = "";
	private StringBuffer buf = new StringBuffer();
	private String treename = "";

	/* 將轉換成List<News>的XML資料回傳 */
	public List<TreeSingleBean> getParsedData() {
		return list;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (list == null) {
			list = new ArrayList<TreeSingleBean>();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
		if (localName.equals("tree_name")) {
			this.in_tree_name = true;
		}
//		
//		if (in_result == true) {
			if (localName.equals("item")) {
				this.in_item = true;
				/* 解析到item的開頭時new一個News物件 */
				mSingleInfo = new TreeSingleBean();
				Log.d("ken", buf.toString());
			} else if (localName.equals("mid")) {
				if (this.in_item) {
					this.in_mid = true;
				} 
				else {
					this.in_description = true;
				}
			} else if (localName.equals("name")) {
				if (this.in_item) {
					this.in_name = true;
				}
			} else if (localName.equals("date")) {
				if (this.in_item) {
					this.in_date = true;
				}
			} else if (localName.equals("description")) {
				if (this.in_item) {
					this.in_description = true;
				}
			} 
//			else if (localName.equals("today_score")) {
//				if (this.in_item) {
//					this.in_today_score = true;
//				}
//			}  else if (localName.equals("fruit_get")) {
//				if (this.in_item) {
//					this.in_fruit_get = true;
//				}
//			}  else if (localName.equals("fruit_start")) {
//				if (this.in_item) {
//					this.in_fruit_start = true;
//				}
//			}  else if (localName.equals("fruit_end")) {
//				if (this.in_item) {
//					this.in_fruit_end = true;
//				}
//			}
//		}
		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		
		if (localName.equals("tree_name")) {
			this.in_tree_name = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			treename = buf.toString().trim();
			buf.setLength(0);
		}
				
		if (localName.equals("item")) {
			this.in_item = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			mSingleInfo.setTreeName(treename);
			
			list.add(mSingleInfo);
		} else if (localName.equals("mid")) {
			if (this.in_item) {
				/* 設定News物件的title */
				mSingleInfo.setItemMid(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_mid = false;
			}
		} else if (localName.equals("name")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mSingleInfo.setItemName(buf.toString().trim());
				buf.setLength(0);
				this.in_name = false;
			}
		} else if (localName.equals("date")) {
			if (in_item) {
				mSingleInfo.setItemDate(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_date = false;
			}
		} else if (localName.equals("description")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSingleInfo.setItemDescription(buf.toString().trim());
				buf.setLength(0);
				this.in_description = false;
			}
		} 
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item || this.in_description || this.in_tree_name) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}